package sunsun.xiaoli.jiarebang.device.pondteam;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Html;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.pondteam.base.BaseActivity;
import com.itboye.pondteam.presenter.UserPresenter;
import com.itboye.pondteam.utils.Const;
import com.itboye.pondteam.utils.TimeCount;
import com.itboye.pondteam.utils.loadingutil.MAlert;
import com.itboye.pondteam.volley.ResultEntity;

import java.util.Observable;
import java.util.Observer;

import org.jetbrains.annotations.NotNull;
import sunsun.xiaoli.jiarebang.R;
import sunsun.xiaoli.jiarebang.custom.ClearEditText;
import sunsun.xiaoli.jiarebang.utils.AreaCodeSelectHelper;
import sunsun.xiaoli.jiarebang.utils.IAreaCodeSelect;

import static com.itboye.pondteam.utils.EmptyUtil.getCustomText;
import static com.itboye.pondteam.utils.EmptyUtil.isEmpty;


/**
 * Created by itboye on 2017/2/24.
 */

public class PondTeamRegisterActivity extends BaseActivity implements Observer, IAreaCodeSelect {

    ClearEditText cleEdEmail;//邮箱
    ClearEditText clearPassword;//登录密码
    ClearEditText clearUserName;//用户呢称
    ClearEditText clearAddress;//地址信息
    ClearEditText clearPhone;//联系电话
    ClearEditText edi_code;//有验证码
    private Button btnDetermine, btncancel;
    UserPresenter userPresenter;
    TextView txt_title;
    ImageView img_back;
    TextView bottom_icon, txt_getYzm;
    private TimeCount time;// 倒计时
    private AlertDialog.Builder alert;
    private AlertDialog dialog;
    TextView clearUserCountry;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pondteam_register_new);
        userPresenter = new UserPresenter(this);
        userPresenter.setBaseUrl(Const.pondlink_wrapUrl);
        txt_title.setText(getApplicationContext().getString(R.string.newuser_register));
        alert = new AlertDialog.Builder(this);
        View view = View.inflate(this, R.layout.alert_use_note, null);
        alert.setView(view);
        dialog = alert.show();
        ((TextView) view.findViewById(R.id.tv_note)).setText(getClickableSpan());
        ((TextView) view.findViewById(R.id.tv_note)).setMovementMethod(LinkMovementMethod.getInstance());
        view.findViewById(R.id.tv_ok).setOnClickListener(this);
        view.findViewById(R.id.tv_cancel).setOnClickListener(this);
    }

    private SpannableString getClickableSpan() {

        //监听器
        View.OnClickListener listener = v -> {
            Uri uri = Uri.parse(Const.PONDLINK_XIYI_URL);
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        };
        SpannableString spanableInfo = new SpannableString(Html.fromHtml(getApplicationContext().getString(R.string.xieyi)));
//        int start = Html.fromHtml(getApplicationContext().getString(R.string.xieyi)).toString().indexOf("http");  //超链接起始位置
//        int end = Html.fromHtml(getApplicationContext().getString(R.string.xieyi)).toString().length() - 1;   //超链接结束位置
//        //可以为多部分设置超链接
//        spanableInfo.setSpan(new Clickable(listener), start, end, Spanned.SPAN_MARK_MARK);
        return spanableInfo;
    }

    @Override
    public void selectFinish(@NotNull String code) {

    }

    class Clickable extends ClickableSpan implements View.OnClickListener {
        private final View.OnClickListener mListener;

        public Clickable(View.OnClickListener listener) {
            mListener = listener;
        }

        @Override
        public void onClick(View view) {
            mListener.onClick(view);
        }

    }

    @Override
    public void onClick(View view) {
        if (R.id.img_back == view.getId() || R.id.btncancel == view.getId()) {
            finish();
        } else if (view.getId() == R.id.tv_ok) {
            dialog.dismiss();
        } else if (view.getId() == R.id.tv_cancel) {
            dialog.dismiss();
            finish();
        } else if (view.getId() == R.id.txt_getYzm) {
            if (isEmpty(cleEdEmail)) {
                MAlert.alert(getApplicationContext().getString(R.string.email_empty));
                return;
            }
            showProgressDialog(getApplicationContext().getString(R.string.requesting), true);
            time = new TimeCount(60000, 1000, txt_getYzm);// 构造CountDownTimer对象
            time.start();
            userPresenter.sendEmailCode(getCustomText(cleEdEmail), 1, 1);
        } else if (R.id.btnDetermine == view.getId()) {
            if (isEmpty(cleEdEmail.getText().toString())) {
                MAlert.alert(getApplicationContext().getString(R.string.email_empty));
                return;
            }
            if (isEmpty(clearPassword.getText().toString())) {
                MAlert.alert(getApplicationContext().getString(R.string.pass_empty));
                return;
            }
            if (isEmpty(edi_code.getText().toString())) {
                MAlert.alert(getApplicationContext().getString(R.string.yzm_empty));
                return;
            }
//            if (isEmpty(clearUserCountry.getText().toString())) {
//                MAlert.alert("Country is empty");
//                return;
//            }
//                if (isEmpty(clearUserName)) {
//                    MAlert.alert(getString(R.string.email_empty));
//                    return;
//                }
//                if (isEmpty(clearAddress)) {
//                    MAlert.alert(getString(R.string.email_empty));
//                    return;
//                }
//                if (isEmpty(clearPhone)) {
//                    MAlert.alert(getString(R.string.email_empty));
//                    return;
//                }

//            if (EmailUtil.emailValidation(cleEdEmail.getText().toString()) != true) {
//                MAlert.alert("邮箱格式不对");
//                return;
//            }
            String email = getCustomText(cleEdEmail);
            String password = getCustomText(clearPassword);
            String nickName = getCustomText(clearUserName);
            String country = getCustomText(clearUserCountry);
            String address = getCustomText(clearAddress);
            String phone = getCustomText(clearPhone);
            String code = getCustomText(edi_code);
            userPresenter.registerByEmail(nickName, phone, country, address, email, password, code);
        } else if (R.id.clearUserCountry == view.getId()) {//选择国家
            new AreaCodeSelectHelper().showCountry(this, R.layout.item_choose_code, clearUserCountry, this);
        }
    }

    @Override
    public void update(Observable o, Object data) {
        ResultEntity resultEntity = handlerError(data);
        try {
            closeProgressDialog();
        } catch (Exception w) {

        }

        if (resultEntity != null) {
            if (resultEntity.getCode() != 0) {
                MAlert.alert(resultEntity.getMsg());
                return;
            }
            if (resultEntity.getEventType() == UserPresenter.register_success) {
                MAlert.alert(resultEntity.getData());
                finish();
            } else if (resultEntity.getEventType() == UserPresenter.register_success) {
                MAlert.alert(resultEntity.getData());
            } else if (resultEntity.getEventType() == UserPresenter.send_code_success) {
                MAlert.alert(resultEntity.getData());

            } else if (resultEntity.getEventType() == UserPresenter.send_code_fail) {
                MAlert.alert(resultEntity.getData());
            }
        }
    }
}
